package bb.lanxing.activity.sport;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import androidx.annotation.NonNull;
import com.baidu.mapapi.map.offline.MKOLSearchRecord;
import com.baidu.mapapi.map.offline.MKOfflineMap;
import com.baidu.mapapi.map.offline.MKOfflineMapListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import bb.lanxing.R;
import bb.lanxing.activity.base.BaseActivity;
import bb.lanxing.util.CommonUtil;

public class BDOfflineProvincesActivity extends BaseActivity implements MKOfflineMapListener {
    private MKOfflineMap offline;

    @Override
    public void onGetOfflineMapState(int i, int i2) {
    }

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.province_list);
        setupActionBar(true, -1, true);
        this.offline = BDOfflineManageActivity.offline;
        new FetchDataTask().execute();
    }

    @Override
    public void onResume() {
        super.onResume();
        this.offline.init(this);
    }

    public void update(final ArrayList<HashMap<String, Object>> arrayList) {
        ListView listView = (ListView) findViewById(R.id.provinceListView);
        listView.setAdapter((ListAdapter) new SimpleAdapter(this, arrayList, R.layout.province_item, new String[]{"name", "size"}, new int[]{R.id.province, R.id.size}));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long j) {
                Integer valueOf = Integer.parseInt(((HashMap) arrayList.get(i)).get("id").toString());
                String obj = ((HashMap) arrayList.get(i)).get("name").toString();
                Intent intent = new Intent();
                intent.putExtra("id", valueOf.intValue());
                intent.putExtra("name", obj);
                intent.setClass(BDOfflineProvincesActivity.this, BDOfflineCitiesActivity.class);
                BDOfflineProvincesActivity.this.startActivityForResult(intent, 1);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_map_offline_bd_province, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem menuItem) {
        if (menuItem.getItemId() == R.id.map_offline_menu_help) {
            Intent intent = new Intent();
            intent.putExtra("layout", R.layout.help_offline_map);
            intent.putExtra("title", getString(R.string.map_offline_download_help));
            intent.setClass(this, HelpActivity.class);
            startActivity(intent);
        }
        return super.onOptionsItemSelected(menuItem);
    }

    @Override
    public void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
        if (i2 == -1 && i == 1) {
            setResult(-1);
        }
    }

    private class FetchDataTask extends AsyncTask<Void, Integer, ArrayList<HashMap<String, Object>>> {
        private FetchDataTask() {
        }

        @Override
        public void onPostExecute(ArrayList<HashMap<String, Object>> arrayList) {
            try {
                BDOfflineProvincesActivity.this.update(arrayList);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public ArrayList<HashMap<String, Object>> doInBackground(Void... voidArr) {
            ArrayList<HashMap<String, Object>> arrayList = new ArrayList<>();
            ArrayList<MKOLSearchRecord> offlineCityList = BDOfflineProvincesActivity.this.offline.getOfflineCityList();
            if (offlineCityList != null) {
                Iterator<MKOLSearchRecord> it = offlineCityList.iterator();
                while (it.hasNext()) {
                    arrayList.add(getListEntry(it.next()));
                }
            }
            return arrayList;
        }

        private HashMap<String, Object> getListEntry(MKOLSearchRecord mKOLSearchRecord) {
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("id", mKOLSearchRecord.cityID);
            hashMap.put("name", mKOLSearchRecord.cityName);
            hashMap.put("type", mKOLSearchRecord.cityType);
            hashMap.put("size", CommonUtil.getSizeString(mKOLSearchRecord.dataSize));
            return hashMap;
        }
    }
}